package com.quanxiaoha.weblog.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quanxiaoha.weblog.admin.convert.AiSettingsConvert;
import com.quanxiaoha.weblog.admin.model.vo.aisettings.FindAiSettingsRspVO;
import com.quanxiaoha.weblog.admin.model.vo.aisettings.UpdateAiSettingsReqVO;
import com.quanxiaoha.weblog.admin.service.AdminAiSettingsService;
import com.quanxiaoha.weblog.common.domain.dos.BlogSettingsDO;
import com.quanxiaoha.weblog.common.domain.mapper.BlogSettingsMapper;
import com.quanxiaoha.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2025-01-10
 * @description: AI设置服务实现
 **/
@Service
@Slf4j
public class AdminAiSettingsServiceImpl extends ServiceImpl<BlogSettingsMapper, BlogSettingsDO> implements AdminAiSettingsService {

    @Autowired
    private BlogSettingsMapper blogSettingsMapper;

    @Override
    public Response updateAiSettings(UpdateAiSettingsReqVO updateAiSettingsReqVO) {
        try {
            // 查询现有的博客设置记录
            BlogSettingsDO existingSettings = blogSettingsMapper.selectById(1L);
            
            if (existingSettings == null) {
                // 如果不存在，创建新记录
                existingSettings = BlogSettingsDO.builder()
                        .id(1L)
                        .name("博客名称")
                        .author("作者")
                        .introduction("介绍")
                        .logo("")
                        .avatar("")
                        .githubHomepage("")
                        .csdnHomepage("")
                        .giteeHomepage("")
                        .zhihuHomepage("")
                        .mail("")
                        .isCommentSensiWordOpen(true)
                        .isCommentExamineOpen(false)
                        .build();
            }
            
            // 更新AI配置字段
            existingSettings.setAiEnabled(updateAiSettingsReqVO.getAiEnabled());
            existingSettings.setAiApiUrl(updateAiSettingsReqVO.getAiApiUrl());
            existingSettings.setAiApiKey(updateAiSettingsReqVO.getAiApiKey());
            existingSettings.setAiModel(updateAiSettingsReqVO.getAiModel());
            existingSettings.setAiTimeout(updateAiSettingsReqVO.getAiTimeout());
            existingSettings.setAiMaxRetries(updateAiSettingsReqVO.getAiMaxRetries());
            existingSettings.setAiStreamEnabled(updateAiSettingsReqVO.getAiStreamEnabled());

            // 保存或更新
            saveOrUpdate(existingSettings);
            
            log.info("AI设置更新成功: {}", updateAiSettingsReqVO);
            return Response.success();
        } catch (Exception e) {
            log.error("更新AI设置失败", e);
            return Response.fail("更新AI设置失败");
        }
    }

    @Override
    public Response findDetail() {
        try {
            // 查询博客设置信息（约定的 ID 为 1）
            BlogSettingsDO blogSettingsDO = blogSettingsMapper.selectById(1L);
            
            if (blogSettingsDO == null) {
                // 如果不存在，返回默认配置（OpenAI格式）
                FindAiSettingsRspVO defaultSettings = FindAiSettingsRspVO.builder()
                        .aiEnabled(true)
                        .aiApiUrl("https://api.openai.com/v1")
                        .aiApiKey("")
                        .aiModel("gpt-3.5-turbo")
                        .aiTimeout(30)
                        .aiMaxRetries(3)
                        .aiStreamEnabled(false)
                        .build();
                return Response.success(defaultSettings);
            }

            // DO 转 VO
            FindAiSettingsRspVO vo = AiSettingsConvert.INSTANCE.convertDO2VO(blogSettingsDO);
            
            return Response.success(vo);
        } catch (Exception e) {
            log.error("获取AI设置详情失败", e);
            return Response.fail("获取AI设置详情失败");
        }
    }
}